package teehee.UI;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import teehee.DB.DBHistory;
import teehee.Manager.HistoryManager;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Class containing methods for the Calendar History Page
 * @author Shub
 */
public class HistoryPage extends Activity{
	final int N = 40; // # of textviews to add

	final TextView[] myTextViews = new TextView[N]; // create empty array;
	LinearLayout historyp;
	Calendar c = Calendar.getInstance();
	SimpleDateFormat f1 = new SimpleDateFormat("MM-dd-yyyy");
	SimpleDateFormat f2 = new SimpleDateFormat("MMddyyyy");
	
	@SuppressWarnings("static-access")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        historyp = (LinearLayout)this.findViewById(R.id.HistoryLayout);
	
        for (int i = 0; i < N; i+=2) {
        	// create a new textview
        	final TextView rowTextView1 = new TextView(this);	    
        	final TextView rowTextView2 = new TextView(this);
	    
        	String date1 = f1.format(c.getTime());
        	String date2 = f2.format(c.getTime());
	    
        	ArrayList<DBHistory> list = HistoryManager.getHistoryByDate(date2);
        	String history = date1 + ":";
	    
        	rowTextView1.setText(history);
        	rowTextView1.setTextSize(18);
        	rowTextView1.setTextColor(Color.parseColor("#093055"));
	    
        	historyp.addView(rowTextView1);
        	history = "";
	    
        	for(int j = 0; j<list.size(); j++)
        	{
        		DBHistory entry = list.get(j);
        		history = history + HistoryManager.getName( entry ) +'\n';

        		if(!entry.getReps().equals("0"))
        			history = history+"Reps: "+HistoryManager.getReps( entry )+'\n';	    	
        		if(!entry.getTime().equals("0"))
        			history = history+"Time: "+HistoryManager.getTime( entry )+'\n';
        		if(!entry.getWeights().equals("0"))
        			history = history+"Weights: "+HistoryManager.getWeights( entry )+'\n';
        		if(!entry.getComments().equals("0"))
        			history = history+"Comments: "+HistoryManager.getComments( entry )
        						+'\n'+'\n';
        	}
        	rowTextView2.setText(history);
        	rowTextView2.setTextColor(Color.parseColor("#1D5285"));
        	
        	// add the textview to the linearlayout
        	historyp.addView(rowTextView2);
	    
        	// save a reference to the textview for later
        	//myTextViews[i] = rowTextView1;
        	//myTextViews[i+1] = rowTextView2;
        	c.add(c.DATE, -1);
        }
	}
	
	@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_MENU))
        { 
        	//setContentView(R.layout.main);
        	Intent homep = new Intent(HistoryPage.this, MainPage.class);
    		startActivity(homep);
            return true;
        }
        
        if ((keyCode == KeyEvent.KEYCODE_SEARCH))
        { 
        	//setContentView(R.layout.main);
        	Intent homep = new Intent(HistoryPage.this, SearchPage.class);
    		startActivity(homep);
            return true;
        }
        
        return super.onKeyDown(keyCode, event);    
    } 
}
